﻿using Dapper_Core.BLL.Interface;
using Dapper_Core.Common;
using Dapper_Core.DAL.Impl;
using Dapper_Core.Table;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Linq;
using Audit.Core;
using Dapper_Core.Model;
using Newtonsoft.Json;
using ExpressionToWhere;

namespace Dapper_Core.BLL.Impl
{
    public class Sys_JobBusiness : BaseRepository<Sys_Job>, ISys_JobBusiness
    {
        public Sys_JobBusiness()
        {

        }

        public Sys_JobBusiness(TransactionContext transactionContext) : base(transactionContext)
        {

        }

        public bool Add(Sys_Job job)
        {
            using (AuditScope scope = AuditScope.Create("Sys_Job:Add", () => job))
            {
                try
                {
                    scope.Comment("添加单个系统定时任务");
                    scope.SetCustomField("Module", "Sys_Job");
                    scope.SetCustomField("Operator", SessionUtility.GetBasicUserInfo().PinyinCode);
                    scope.SetCustomField("Operation", Operation.Insert);
                    bool success = base.Add(job);
                    if (!success)
                    {
                        scope.Discard();
                    }
                    return success;
                }
                catch (Exception ex)
                {
                    scope.Discard();
                    NlogUtility.WriteError(MethodBase.GetCurrentMethod().DeclaringType.FullName, "系统定时任务添加失败", "role=" + JsonConvert.SerializeObject(job), ex, "DapperCore");
                    return false;
                }
            }
        }

        public (List<Sys_Job> roles, int count) GetPageList(int pageIndex, int rowsPerPage, string search, string orderby)
        {
            try
            {
                if (pageIndex < 0 || rowsPerPage < 1)
                {
                    return (new List<Sys_Job>(), 0);
                }
                Expression<Func<Sys_Job, bool>> exp = job => job.JobName.Contains(search) || job.JobId.Contains(search);
                List<Sys_Job> jobs = base.GetPagelist(pageIndex + 1, rowsPerPage, exp, orderby).ToList();
                int count = base.GetListCount(exp);
                NlogUtility.WriteInfo(MethodBase.GetCurrentMethod().DeclaringType.FullName, "查询系统定时任务分页信息", "pageIndex=" + pageIndex + "，rowsPerPage=" + rowsPerPage + "，search=" + search + "，orderby=" + orderby, "DapperCore");
                return (jobs, count);
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "获取定时任务分页信息失败", "pageIndex=" + pageIndex + "，rowsPerPage=" + rowsPerPage + "，search=" + search, ex, "DapperCore");
                return (new List<Sys_Job>(), 0);
            }
        }

        public bool Delete(string ids, string deleter)
        {
            using (AuditScope scope = AuditScope.Create("Sys_Job:Delete", () => ids))
            {
                try
                {
                    if (string.IsNullOrEmpty(ids)) return true;
                    List<string> idList = ids.Split(',', StringSplitOptions.RemoveEmptyEntries).ToList();
                    if (idList.Count < 1) return true;
                    scope.Comment("删除系统定时任务");
                    scope.SetCustomField("Module", "Sys_Job");
                    scope.SetCustomField("Operator", deleter);
                    scope.SetCustomField("Operation", Operation.Delete);
                    //定义时间变量，保证同一次删除数据的删除时间相等
                    DateTime dateTime = DateTime.Now;
                    Expression<Func<Sys_Job, bool>> exp = job => job.JobId.In(idList);
                    bool success = base.DeleteList(exp);
                    if (!success)
                    {
                        scope.Discard();
                    }
                    return success;
                }
                catch (Exception ex)
                {
                    scope.Discard();
                    NlogUtility.WriteError(MethodBase.GetCurrentMethod().DeclaringType.FullName, "系统定时任务删除失败", "ids=" + ids, ex, "DapperCore");
                    return false;
                }
            }
        }
    }
}
